<?php

declare(strict_types=1);

namespace app\admin\service\shop;

use app\common\model\circle\CircleModel;
use app\common\model\dynamic\DynamicModel;
use app\common\model\goods\GoodsModel;
use Dengje\Pxy\BaseService;

class BannerService extends BaseService
{
    protected $model = null;


    public function __construct()
    {
        $this->model =  new \app\common\model\BannerModel();
    }

    public function list()
    {
        $where = [];
        $status = request()->get('status', 0);
        if ($status) $where[] = ['status', '=', $status];
        $list = $this->model->where($where)->order(['weigh' => 'desc', 'id' => 'desc'])->paginate(request()->get('page_size/d', 10));
        $list->map(function ($v) {
            if ($v['type'] == 1) {
                $v['type_str'] = DynamicModel::where('id', $v['type_id'])->value('content');
            } elseif ($v['type'] == 2) {
                $v['type_str'] = GoodsModel::where('id', $v['type_id'])->value('name');
            } elseif ($v['type'] == 3) {
                $v['type_str'] = CircleModel::where('id', $v['type_id'])->value('name');
            } elseif ($v['type'] == 4) {
                $v['type_str'] = '卡券页';
            } else {
                $v['type_str'] = '不跳转';
            }
            return $v;
        });
        return $list;
    }

    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        return $detail;
    }

    public function type_list($type)
    {
        if ($type == 1) {
            $where[] = ['status', '=', 2];
            $list = DynamicModel::where($where)->field('id,content AS name')->select();
        } elseif ($type == 2) {
            $where[] = ['status', '=', 1];
            $list = GoodsModel::where($where)->field('id,name')->select();
        } elseif ($type == 3) {
            $where[] = ['status', 'in', [1, 2]];
            $list = CircleModel::where($where)->field('id,name')->select();
        }
        return $list ?? [];
    }

    public function create($data)
    {
        if ($data['type'] == 1) {
            $data['type_url'] = '/pages/note/details?id=' . $data['type_id'];
        } elseif ($data['type'] == 2) {
            $data['type_url'] = '/pages/goods/details?id=' . $data['type_id'];
        } elseif ($data['type'] == 3) {
            $data['type_url'] = '/pages/note/circle?id=' . $data['type_id'];
        } elseif ($data['type'] == 4) {
            $data['type_url'] = '/pages/center/card';
        }
        $banner = $this->model->create($data);
        return $banner;
    }

    public function update($id, $data)
    {
        $data = $this->preExcludeFields($data);
        if ($data['type'] == 1) {
            $data['type_url'] = '/pages/note/details?id=' . $data['type_id'];
        } elseif ($data['type'] == 2) {
            $data['type_url'] = '/pages/goods/details?id=' . $data['type_id'];
        } elseif ($data['type'] == 3) {
            $data['type_url'] = '/pages/note/circle?id=' . $data['type_id'];
        } elseif ($data['type'] == 4) {
            $data['type_url'] = '/pages/center/card';
        }
        return $this->model->where('id', $id)->update($data);
    }

    public function delete($id)
    {
        $this->model->where('id', $id)->delete();
        return success('删除成功！');
    }
}
